package uk.co.biggroup.lotto.syndicate;

import java.util.ArrayList;

import uk.co.biggroup.lotto.syndicate.dataset.DataDraws;
import uk.co.biggroup.lotto.syndicate.dataset.DataLine;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Numbers extends ListActivity {

	public static final String PREFS_NAME = "Settings";
	public static int error;

	static int numberOfSets;
	
	TextView statusstatus;
	TextView drawdate;	
	TextView[] no;
	ImageView icon6;
	RelativeLayout ballsset;
	
	public static int drawid;
	DataDraws dd;
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.numbers);
		Bundle bundle = this.getIntent().getExtras();

		drawid = bundle.getInt("drawid");
		dd = (DataDraws) bundle.getSerializable("DataSet");
		
		Toast.makeText(this, dd.getDescr(), Toast.LENGTH_LONG).show();
		Logh.i("START NUMBERS");
		ballsset = (RelativeLayout) this.findViewById(R.id.ballsset);
		
		if (dd.getDataDraw().get(drawid).getType() == 2) {
			icon6 = (ImageView) this.findViewById(R.id.icon6);
			icon6.setImageResource(R.drawable.ball_aqua_icon);
		}
		Logh.i("START NUMBERS 2");
//		ArrayList<DataLine> DataLines = dd.getDataDraw().get(drawid).getDataLines();
		if (dd.getDataDraw().get(drawid).getResult().length() == 0) {
			ballsset.setVisibility(View.GONE);
//			lnumber = new LuckyNumbers("00,00,00,00,00,00,00");
		} else {
			ballsset.setVisibility(View.VISIBLE);
//			lnumber = new LuckyNumbers(dd.getDataDraw().get(drawid).getResult());
		}
		Logh.i("START NUMBERS 3");
		
		statusstatus = (TextView) this.findViewById(R.id.statusstatus);
		drawdate = (TextView) this.findViewById(R.id.drawdate);
	
		Logh.i("START NUMBERS 4");
		no = new TextView[7];
		no[0] = (TextView) this.findViewById(R.id.no1);
		no[1] = (TextView) this.findViewById(R.id.no2);
		no[2] = (TextView) this.findViewById(R.id.no3);
		no[3] = (TextView) this.findViewById(R.id.no4);
		no[4] = (TextView) this.findViewById(R.id.no5);
		no[5] = (TextView) this.findViewById(R.id.no6);
		no[6] = (TextView) this.findViewById(R.id.no7);		
		
		Logh.i("START NUMBERS 5");
		for(int i = 0; i < dd.getDataDraw().get(drawid).getNoResultNumbers(); i++) {
			no[i].setText(dd.getDataDraw().get(drawid).getResultSingle(i));	
		}
		Logh.i("START NUMBERS 6");
		drawdate.setText(dd.getDataDraw().get(drawid).getDate() + " ");
		statusstatus.setText(dd.getDataDraw().get(drawid).getStatus() + " ");

		Logh.i("START NUMBERS 7");
		Numbers.this.setListAdapter(new IconicAdapter(Numbers.this,
				R.layout.numbersitem, dd.getDataDraw().get(drawid).getDataLines()));
	}

	class IconicAdapter extends ArrayAdapter<DataLine> { 
		
		private ArrayList<DataLine> items;

		IconicAdapter(Context context, int textViewResourceId,
				ArrayList<DataLine> itemsyx) {
			super(context, textViewResourceId, itemsyx);
			this.items = itemsyx;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			
			LayoutInflater inflater = getLayoutInflater();
			View drawitem = inflater.inflate(R.layout.numbersitem, parent, false);
			TextView matches = (TextView) drawitem.findViewById(R.id.matches);
			TextView counter = (TextView) drawitem.findViewById(R.id.counter);
			
			TextView[] lno = new TextView[6];
			lno[0] = (TextView) drawitem.findViewById(R.id.no1);
			lno[1] = (TextView) drawitem.findViewById(R.id.no2);
			lno[2] = (TextView) drawitem.findViewById(R.id.no3);
			lno[3] = (TextView) drawitem.findViewById(R.id.no4);
			lno[4] = (TextView) drawitem.findViewById(R.id.no5);
			lno[5] = (TextView) drawitem.findViewById(R.id.no6);
			
			ImageView[] licon = new ImageView[6];
			licon[0] = (ImageView) drawitem.findViewById(R.id.icon);
			licon[1] = (ImageView) drawitem.findViewById(R.id.icon2);
			licon[2] = (ImageView) drawitem.findViewById(R.id.icon3);
			licon[3] = (ImageView) drawitem.findViewById(R.id.icon4);
			licon[4] = (ImageView) drawitem.findViewById(R.id.icon5);
			licon[5] = (ImageView) drawitem.findViewById(R.id.icon6);

			ImageView star = (ImageView) drawitem.findViewById(R.id.star);
			Logh.i("START NUMBERS 8 POS: " + position);
			DataLine dl = items.get(position);
			if (dl != null) {
				int matchCounter = 0;
				
				for(int i = 0; i < dl.getNoNumbers(); i++) {
					Logh.i("START NUMBERS x POS: " + i + " NO: " + dl.getNumberString(i));
					lno[i].setText(dl.getNumberString(i));
					if (dl.getHit(i)) {
						licon[i].setImageResource(R.drawable.ball_red_icon);
						matchCounter++;
					}	
				}

				int dpos = position + 1;
				counter.setText(Integer.toString(dpos));
				matches.setText(Integer.toString(matchCounter));
				if(matchCounter >= 3) {
					star.setVisibility(View.VISIBLE);
				}
			}
			return (drawitem);
			
		}
	}	
}
